/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.itttq.gshop.domain.po.OrderInfoPo;
import com.itttq.gshop.domain.to.IndexStatTo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomInLanguage;
import com.itttq.gshop.pbpm.provider.OrderInfoSqlProvider;

@Mapper
public interface IOrderInfoMapper {
	/**
	 * 新增OrderInfoPo对象，并设置生成的ID到orderInfo对象中
	 * 
	 * @param OrderInfoPo
	 * @Author 迷蝶
	 * @return Integer
	 */
	@InsertProvider(type=OrderInfoSqlProvider.class,  method="addOrderInfo") 
	@Options(useGeneratedKeys = true, keyProperty = "id")
	public Integer addOrderInfo(OrderInfoPo orderInfo);

	/**
	 * 更新OrderInfoPo对象
	 * 
	 * @param orderInfoPo
	 * @Author 迷蝶
	 * @return Integer
	 */
	@UpdateProvider(type=OrderInfoSqlProvider.class,  method="updateOrderInfo") 
	public Integer updateOrderInfo(OrderInfoPo orderInfo);

	/**
	 * 根据订单ID更新订单状态
	 * 
	 * @param id	订单ID
	 * @param status	订单更新后的状态
	 * @param statusName	订单更新后的状态名称
	 * @Author 迷蝶
	 * @return Integer
	 */
	@Update("UPDATE order_info set status= #{status}, status_name=#{statusName} WHERE id =#{id} and status in (${sourceStatus})")
	public Integer updateOrderStatus(@Param("id") int id, @Param("status") int status, 
			@Param("statusName") String statusName, @Param("sourceStatus") String sourceStatus);
	
	/**
	 * 批量更新订单状态，按订单时间是否大于指定时间来取消
	 * 
	 * @param statusCurrent	当前订单状态，作为限制使用，避免错乱更新订单状态，比如已完成的被更新为未付款
	 * @param endTime			指定时间，当下单时间大于此时间的订单才更新
	 * @Author 迷蝶
	 * @return Integer
	 */
	@Select("SELECT id FROM order_info WHERE `status`= #{statusCurrent} AND order_time < #{endTime} ")
	public List<Integer> findOrderStatusByTime(@Param("statusCurrent") int statusCurrent, @Param("endTime") Date endTime);
	
	/**
	 * 
	 * 批量更新订单状态，按订单时间是否大于指定时间来取消
	 * 
	 * @param status				更新后的订单状态
	 * @param statusName	状态名称
	 * @param statusCurrent	当前订单状态，更新前，此状态为限制订单状态只能有指定状态更新到下一状态。
	 * @param ids					要更新的订单ID
	 * @Author 迷蝶
	 * @return Integer
	 */
	@Lang(CustomInLanguage.class)
	@Update("UPDATE order_info set status=#{status}, status_name=#{statusName} WHERE `status`= #{statusCurrent} AND id IN (#{ids})")
	public Integer updateOrderStatusByTime(@Param("status") int status, @Param("statusName") String statusName, 
			@Param("statusCurrent")  int statusCurrent,  @Param("ids") List<Integer> ids);
	
	/**
	 * 根据ID删除OrderInfoPo对象
	 * 
	 * @param id
	 * @Author 迷蝶
	 * @return Integer
	 */
	@DeleteProvider(type=OrderInfoSqlProvider.class,  method="delOrderInfo") 
	public Integer delOrderInfoById(Integer id);

	/**
	 * 根据IDS删除OrderInfoPo对象
	 * 
	 * @param ids
	 * @Author 迷蝶
	 * @return List<OrderInfoPo>
	 */
	@Lang(CustomInLanguage.class)
	@Delete("DELETE FROM order_info WHERE id IN (#{ids})")
	public Integer delOrderInfoByIds(@Param("ids") List<Integer> ids);
	
	/**
	 * 根据条件查找OrderInfoPo对象，默认根据ID降序
	 * 
	 * @param orderInfoPo
	 * @Author 迷蝶
	 * @return List<OrderInfoPo>
	 */
	@SelectProvider(type=OrderInfoSqlProvider.class,  method="findOrderInfos") 
	public List<OrderInfoPo> findOrderInfos(OrderInfoPo orderInfo);
	
	/**
	 * 根据条件查找记录总数
	 * 
	 * @param orderInfoPo
	 * @Author 迷蝶
	 * @return Integer
	 */
	@SelectProvider(type=OrderInfoSqlProvider.class,  method="findOrderInfoCount") 
	public Integer findOrderInfoCount(OrderInfoPo orderInfo);

	/**
	 * 根据ID查找OrderInfoPo对象
	 * 
	 * @param id
	 * @Author 迷蝶
	 * @return OrderInfoPo
	 */
	@SelectProvider(type=OrderInfoSqlProvider.class,  method="findOrderInfoById") 
	public OrderInfoPo findOrderInfoById(Integer id);

	/**
	 * 获取订单全部和当天的统计数据
	 * 
	 * @Author 迷蝶
	 * @return IndexStatTo
	 */
	@Select("select * from (select sum(amount_total) as total, count(*) as count from order_info) t1, (select sum(amount_total) as totalToday, count(*) as countToday from order_info where TO_DAYS(NOW( )) = TO_DAYS(order_time)) t2") 
	public IndexStatTo findOrderStat();
	
	
}